﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
using MyWeb.Data;
namespace MyWeb.Admins
{
    public partial class tblImgAlbum : System.Web.UI.Page
    {
        static bool Insert = false;protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack){Session["folder"] = "tblImgAlbum"; BindGrid();
            }
        }
private void BindGrid()
{
    grdtblImgAlbum.DataSource = tblImgAlbumService.tblImgAlbum_GetByAll() ;
    grdtblImgAlbum.DataBind();}
protected void btnThem_Click(object sender, EventArgs e)
{
    pnShow.Visible = false;
    pnUpdate.Visible = true;
    ControlClass.ResetControlValues(this);
    Session["upload"] = null;Insert=true;
}
protected void btnXoa_Click(object sender, EventArgs e)
{
    try
    {
        DataGridItem item = default(DataGridItem);
        for (int i = 0; i < grdtblImgAlbum.Items.Count; i++)
        {
            item = grdtblImgAlbum.Items[i];
            if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item){if (((CheckBox)item.FindControl("ChkSelect")).Checked)
            {
                string strId = item.Cells[1].Text;
                try
                {
                    tblImgAlbumService.tblImgAlbum_Delete(strId);
                }
                catch{
                }
            }
            }
        }
        grdtblImgAlbum.CurrentPageIndex = 0;BindGrid();
    }
    catch{
    }
}protected void grdtblImgAlbum_ItemCommand(object source, DataGridCommandEventArgs e)
            {
                Data.tblImgAlbumInfo strCA=new tblImgAlbumInfo();
strCA._iaid = e.CommandArgument.ToString();
if  (e.CommandName=="Edit")
{
    DataTable dt = tblImgAlbumService.tblImgAlbum_GetById(strCA);

    dll_uid_Update.SelectedValue = dt.Rows[0]["_uid"].ToString();
    txt_iaid.Text=dt.Rows[0]["_iaid"].ToString();
    txt_ianame.Text=dt.Rows[0]["_ianame"].ToString();
    pnUpdate.Visible = true;pnShow.Visible = false;
}
else
{
    tblImgAlbumService.tblImgAlbum_Delete(strCA._iaid); BindGrid();
}
}
        protected void lblBack_Click(object sender, EventArgs e)
{
   
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            Insert=false;
        }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
            #region[TestInput]
            if (dll_uid_Update.SelectedValue.Trim().Equals(""))
{
    WebMsgBox.Show("_uid không thể trống !");
    dll_uid_Update.Focus();
    return;
}
            if (txt_iaid.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_iaid không thể trống !");
                txt_iaid.Focus();
                return;
            }
            if (txt_ianame.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("_ianame không thể trống !");
                txt_ianame.Focus();
                return;
            }
#endregion
Data.tblImgAlbumInfo obj=new Data.tblImgAlbumInfo();
obj._uid = dll_uid_Update.SelectedValue;
            obj._iaid = txt_iaid.Text;obj._ianame = txt_ianame.Text;
            if (Insert == true)
            {
                tblImgAlbumService.tblImgAlbum_Insert(obj);
            }
            else{tblImgAlbumService.tblImgAlbum_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
}
        protected void grdtblImgAlbum_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdtblImgAlbum.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
        private void ShowDll()
{
            var dt = new DataTable();
dt = tblUserService.tblUser_GetByAll();
dll_uid.DataSource = dt;
            dll_uid.DataTextField = "";
            dll_uid.DataValueField = "";
            dll_uid.DataBind();
            dll_uid_Update.DataSource = dt;
            dll_uid_Update.DataTextField = "";
            dll_uid_Update.DataValueField = "";
            dll_uid_Update.DataBind();
        }
        protected void btnshow_Click(object sender, EventArgs e)
        {
            grdtblImgAlbum.DataSource =tblImgAlbumService.tblImgAlbum_GetBy_uid(dll_uid.SelectedValue);
            grdtblImgAlbum.DataBind();
        }
        protected void grdtblImgAlbum_ItemDataBound(object sender, DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if
                    (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
}
}
                else
                {
string tableRowId = grdtblImgAlbum.ClientID + "_row" + e.Item.ItemIndex.ToString();
e.Item.Attributes.Add("id", tableRowId);
object checkBox = e.Item.FindControl("chkSelect");
if ((checkBox != null))
{e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
    ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
}
                }
            }
        }
}
}